<?php
namespace Api\Controller;

use Common\Controller\ApiController;
use Common\ORG\qiniu;

class WxuploadController extends ApiController {

    private $resume = [];
    private $company= [];

    public function _initialize()
    {
        parent::_initialize();
        if(!IS_POST){
            $this->ajaxReturn(1, '提交方式错误！');
        }

        $this->resume   = $this->isResume();
        $this->company  = $this->isCompany();
    }

    /**
     * 微信上传
     * [fileUpload 附件上传]
     * @return [type] [description]
     */
    public function fileUpload() {
        $type       = I('post.type', 'image', 'trim');
        $fileName   = I('post.name', '', 'trim');//文件名称
        //echo C('qscms_site_domain') . __ROOT__ . '/data/upload/' . $type . '/' . $attach
        if (!in_array($type, array('bu_jiao_pay_img','pay_img','subject_message_img','top_resume_img', 'resume_img', 'top_word_resume', 'word_resume', 'other_word_resume', 'resume_video', 'company_logo', 'company_img', 'certificate_img', 'avatar'))) {
            $this->ajaxReturn(1, '上传类型错误！');
        }
        
        if (!empty($_FILES[$type]['name'])) {
            $this->$type($this->resume['id'], $fileName);
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));//非法参数
        }

    }

    /**
     * [resume_video 上传视频]
     * @return [type] [description]
     */
    protected function resume_video($pid=0, $fileName='') {
        if (!$this->resume) {
            $this->ajaxReturn(1, '会员资料不存在！');
        }
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok'         => false,
            'resume_video_title' => '',
            'save_path'         => '',
            'show_path'         => '',
            'info'              => ''
        );

        $date = date('ym/d');
        $result = $this->_upload($_FILES['resume_video'], 'resume_video/' . $date, array(
            'maxSize'       => 50 * 1024,//word最大2M
            'uploadReplace' => true,
            'attach_exts'   => 'mp4,avi,mov'
        ));

        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date .'/'. $result['info'][0]['savename'];
            $config_params['savepath']  = $result['info'][0]['savepath'].$result['info'][0]['savename'];
            $config_params['show_path'] = U('download/resume_video',array('id'=>$pid));
            $config_params['info'] = '';
            $config_params['resume_video_title'] = !empty($fileName) ? $fileName :  badword($_FILES['resume_video']['name']);
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $resume_mod = M('Resume');
            $where = array('id' => $pid, 'uid' => C('visitor.uid'));
            if (false === $word = $resume_mod->where($where)->getfield('resume_video')) $this->ajaxReturn(0, '简历不存在或已经删除！');
            $save_arr['resume_video'] = $config_params['save_path'];
            $save_arr['resume_video_title'] = !empty($fileName) ? $fileName :  _I($config_params['resume_video_title']);
            $save_arr['resume_video_addtime'] = time();
            $rid = $resume_mod->where($where)->save($save_arr);
            D('Resume')->save_resume('', $pid, C('visitor'));
            @unlink(C('qscms_attach_path') . "resume_video/" . $word);

            $data   = [
                'name'      => $save_arr['resume_video_title'],
                //'path'  => $config_params['show_path'],
                'path_full' => C('qscms_site_domain') . __ROOT__ . '/data/upload/resume_video/' .  $config_params['save_path'],
                'time'      => date('Y-m-d H:i', time())
            ];
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    /**
     * [resume_img 个人简历图片上传]
     * @return [type] [description]
     */
    protected function resume_img($pid=0, $fileName='') {
        if (!$this->resume) {
            $this->ajaxReturn(1, '会员资料不存在！');
        }
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok' => false,
            'img_url'   => '',
            'img_path'  => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['resume_img'], 'resume_img/' . $date, array(
            'maxSize'       => C('qscms_resume_photo_max'),//图片大小上限 最大5M
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ));

        if ($result['error']) {
            $image = new \Common\ORG\ThinkImage();
            $path = $result['info'][0]['savepath'] . $result['info'][0]['savename'];
            $size = explode(',', C('qscms_resume_img_size'));
            foreach ($size as $val) {
                $image->open($path)->thumb($val, $val, 3)->save("{$path}_{$val}x{$val}.jpg");
            }
            $config_params['upload_ok'] = true;
            $config_params['img_url'] = $date . $result['info'][0]['savename'];
            $config_params['img_path'] = attach($config_params['img_url'], 'resume_img');
            $config_params['info'] = '';
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $img_mod = M('ResumeImg');
            $setsqlarr['resume_id'] = $pid;
            $setsqlarr['uid'] = C('visitor.uid');
            $setsqlarr['title'] = '';
            $setsqlarr['img'] = $config_params['img_url'];
            $setsqlarr['id'] = I('post.id', 0, 'intval');
            if ($setsqlarr['id'] == 0) {
                $count = $img_mod->where(array('resume_id' => $pid, 'uid' => C('visitor.uid')))->count('id');
                if ($count >= 6) {
                    $this->ajaxReturn(1, '简历图片最多只可上传6张！');
                    exit;
                }
            }
            $rst = D('ResumeImg')->save_resume_img($setsqlarr);
            $data = array('path_full' => $config_params['img_path'], 'img' => $config_params['img_url'], 'id' => $rst['id']);
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }


    /**
     * [word_resume 上传word简历]
     * @return [type] [description]
     */
    protected function word_resume($pid=0, $fileName='') {
        if (!$this->resume) {
            $this->ajaxReturn(1, '会员资料不存在！');
        }
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok'         => false,
            'word_resume_title' => '',
            'save_path'         => '',
            'show_path'         => '',
            'info'              => ''
        );

        $date = date('ym/d');
        $result = $this->_upload($_FILES['word_resume'], 'word_resume/' . $date, array(
            'maxSize'       => C('qscms_resume_photo_max'),//word最大5M
            'uploadReplace' => true,
            'attach_exts'   => 'doc,docx,pdf'
        ));

        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date .'/'. $result['info'][0]['savename'];
            $config_params['savepath']  = $result['info'][0]['savepath'].$result['info'][0]['savename'];
            $config_params['show_path'] = U('download/word_resume',array('id'=>$pid));
            $config_params['info'] = '';
            $config_params['word_resume_title'] =  !empty($fileName) ? $fileName :  badword($_FILES['word_resume']['name']);
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $resume_mod = M('Resume');
            $where = array('id' => $pid, 'uid' => C('visitor.uid'));
            if (false === $word = $resume_mod->where($where)->getfield('word_resume')) $this->ajaxReturn(0, '简历不存在或已经删除！');
            $save_arr['word_resume'] = $config_params['save_path'];
            $save_arr['word_resume_title'] = _I($config_params['word_resume_title']);
            $save_arr['word_resume_addtime'] = time();
            $rid = $resume_mod->where($where)->save($save_arr);
            D('Resume')->save_resume('', $pid, C('visitor'));
            @unlink(C('qscms_attach_path') . "word_resume/" . $word);

            $data   = [
                'name'      => $save_arr['word_resume_title'],
                //'path'  => $config_params['show_path'],
                'path_full' => C('qscms_site_domain') . __ROOT__ . '/data/upload/word_resume/' .  $config_params['save_path'],
                'time'      => date('Y-m-d H:i', time())
            ];
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    /**
     * [word_resume 上传其他word简历]
     * @return [type] [description]
     */
    protected function other_word_resume($pid=0, $fileName='') {
        if (!$this->resume) {
            $this->ajaxReturn(1, '会员资料不存在！');
        }
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok'         => false,
            'other_word_resume_title' => '',
            'save_path'         => '',
            'show_path'         => '',
            'info'              => ''
        );

        $date = date('ym/d');
        $result = $this->_upload($_FILES['other_word_resume'], 'other_word_resume/' . $date, array(
            'maxSize'       => C('qscms_resume_photo_max'),//word最大5M
            'uploadReplace' => true,
            'attach_exts'   => 'doc,docx,pdf'
        ));

        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date .'/'. $result['info'][0]['savename'];
            $config_params['savepath']  = $result['info'][0]['savepath'].$result['info'][0]['savename'];
            $config_params['show_path'] = U('download/other_word_resume',array('id'=>$pid));
            $config_params['info'] = '';
            $config_params['other_word_resume_title'] =  !empty($fileName) ? $fileName :  badword($_FILES['other_word_resume']['name']);
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $resume_mod = M('Resume');
            $where = array('id' => $pid, 'uid' => C('visitor.uid'));
            if (false === $word = $resume_mod->where($where)->getfield('other_word_resume')) $this->ajaxReturn(0, '简历不存在或已经删除！');
            $save_arr['other_word_resume'] = $config_params['save_path'];
            $save_arr['other_word_resume_title'] = _I($config_params['other_word_resume_title']);
            $save_arr['other_word_resume_addtime'] = time();
            $rid = $resume_mod->where($where)->save($save_arr);
            D('Resume')->save_resume('', $pid, C('visitor'));
            @unlink(C('qscms_attach_path') . "other_word_resume/" . $word);

            $data   = [
                'name'      => $save_arr['other_word_resume_title'],
                //'path'  => $config_params['show_path'],
                'path_full' => C('qscms_site_domain') . __ROOT__ . '/data/upload/other_word_resume/' .  $config_params['save_path'],
                'time'      => date('Y-m-d H:i', time())
            ];
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }




    /**
     * [resume_img 个人简历图片上传]
     * @return [type] [description]
     */
    protected function top_resume_img($pid=0, $fileName='') {
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok' => false,
            'img_url'   => '',
            'img_path'  => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['top_resume_img'], 'top_resume_img/' . $date, array(
            'maxSize'       => C('qscms_resume_photo_max'),//图片大小上限
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ));
        if ($result['error']) {
            $image = new \Common\ORG\ThinkImage();
            $path = $result['info'][0]['savepath'] . $result['info'][0]['savename'];
            $size = explode(',', C('qscms_resume_img_size'));
            foreach ($size as $val) {
                $image->open($path)->thumb($val, $val, 3)->save("{$path}_{$val}x{$val}.jpg");
            }
            $config_params['upload_ok'] = true;
            $config_params['img_url'] = $date . $result['info'][0]['savename'];
            $config_params['img_path'] = attach($config_params['img_url'], 'top_resume_img');
            $config_params['info'] = '';
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $img_mod = M('AdvResumeImg');
            $setsqlarr['resume_id'] = $pid;
            $setsqlarr['uid'] = C('visitor.uid');
            $setsqlarr['title'] = '';
            $setsqlarr['img'] = $config_params['img_url'];
            $setsqlarr['id'] = I('post.id', 0, 'intval');
            if ($setsqlarr['id'] == 0) {
                $count = M('AdvResumeImg')->where(array('resume_id' => $pid, 'uid' => C('visitor.uid')))->count('id');
                if ($count >= 6) {
                    $this->ajaxReturn(0, '简历附件最多只可上传6张！');
                    exit;
                }
            }
            $rst = D('AdvResumeImg')->save_resume_img($setsqlarr);
            $data = array('path' => $config_params['img_path'], 'img' => $config_params['img_url'], 'id' => $rst['id']);
            $this->ajaxReturn(1, L('upload_success'), $data, '', 'HTML');
        } else {
            $this->ajaxReturn(0, $config_params['info']);
        }
    }



    /**
     * [word_resume 上传高级word简历]
     * @return [type] [description]
     */
    protected function top_word_resume($pid=0, $fileName='') {
        $pid = $pid ? $pid : I('post.pid', 0, 'intval');
        $config_params = array(
            'upload_ok'         => false,
            'word_resume_title' => '',
            'save_path'         => '',
            'show_path'         => '',
            'info'              => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['top_word_resume'], 'top_word_resume/' . $date, array(
            'maxSize'       => 2 * 1024,//word最大2M
            'uploadReplace' => true,
            'attach_exts'   => 'doc,docx'
        ));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = U('download/adv_word_resume',array('id'=>$pid));
            $config_params['info'] = '';
            $config_params['word_resume_title'] = badword($_FILES['top_word_resume']['name']);
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $resume_mod = M('AdvResume');
            $where = array('id' => $pid, 'uid' => C('visitor.uid'));
            if (!$word = $resume_mod->where($where)->getfield('word_resume')) $this->ajaxReturn(0, '简历不存在或已经删除！');
            $save_arr['word_resume'] = $config_params['save_path'];
            $save_arr['word_resume_title'] = _I($config_params['word_resume_title']);
            $save_arr['word_resume_addtime'] = time();
            $rid = $resume_mod->where($where)->save($save_arr);
            D('AdvResume')->save_resume('', $pid, C('visitor'));
            @unlink(C('qscms_attach_path') . "top_word_resume/" . $word);

            $this->ajaxReturn(1, L('upload_success'), array('name' => $save_arr['word_resume_title'], 'path' => $config_params['show_path'], 'time' => date('Y-m-d H:i', time())), '', 'HTML');
        } else {
            $this->ajaxReturn(0, $config_params['info']);
        }
    }


    /**
     * [company_logo 企业logo]
     */
    protected function company_logo() {
        $company = $this->company;
        if(empty($company)){
            //$this->ajaxReturn(1, '企业信息不存在');
        }
        $config_params = array(
            'upload_ok' => false,
            'path'      => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['company_logo'], 'company_logo/' . $date, array(
            'maxSize'       => C('qscms_logo_max_size'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ), md5($company['id']));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['path'] = attach($date . $result['info'][0]['savename'], 'company_logo') . '?' . time();
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['info'] = '';
        } else {
            $config_params['info'] = $result['info'];
        }
        if ($config_params['upload_ok']) {
            $rst = M('CompanyProfile')->where(array('id' => $company['id'], 'uid' => C('visitor.uid')))->setfield('logo', $date . $result['info'][0]['savename']);
            $this->ajaxReturn(0, L('upload_success'), ['path_full' => $config_params['path'], 'img' => $config_params['save_path']], '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    protected function company_img($pid=0, $fileName='') {
        $company_id = $this->company;
        if($company_id) {
            $num = M('CompanyImg')->where(array('company_id' => $company_id['id'], 'uid' => C('visitor.uid')))->count();
            if ($num >= 8) $this->ajaxReturn(0, '企业风采不能超过8张！');
        }
        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['company_img'], 'company_img/' . $date, array(
            'maxSize'       => C('qscms_company_img_max'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'company_img');
            $config_params['info'] = '';
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $setsqlarr['uid']           = C('visitor.uid');
            $setsqlarr['company_id']    = $company_id['id'];
            $setsqlarr['img']           = $config_params['save_path'];
            $id = '0';
            if ($this->company) {//企业信息存在则添加
                $rst = D('CompanyImg')->add_company_img($setsqlarr, C('visitor'));
                !$rst['state'] && $this->ajaxReturn(1, $rst['error']);
                $id = $rst['id'];
            }
            //$r = D('TaskLog')->do_task(C('visitor'), 'upload_companyimg');
            $data = array('path_full' => $config_params['show_path'], 'img'=> $config_params['save_path'], 'id'=>$id);
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    // 企业营业执照 上传
    protected function certificate_img($pid=0, $fileName='') {

        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['certificate_img'], 'certificate_img/' . $date, array(
            'maxSize'       => C('qscms_certificate_max_size'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ), md5(C('visitor.uid')));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'certificate_img');
            $config_params['info'] = '';

        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $data['certificate_img'] = $config_params['save_path'];
            $data['audit'] = 2;
            $data['img'] = $config_params['save_path'];
            $data['url'] = $config_params['show_path'];
            if ($this->company) {
                $rst = D('CompanyProfile')->add_certificate_img($data, C('visitor'));
                !$rst['state'] && $this->ajaxReturn(0,$rst['error']);
            }
            unset($data['certificate_img'], $data['audit'], $data['url']);
            $data['path_full']  = $config_params['show_path'];
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    /**
     * [avatar 头像上传保存]
     */
    public function avatar($pid=0, $fileName='') {
        if(empty($this->userinfo)){
            $this->ajaxReturn(1, '用户信息不存在');
        }
        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['avatar'], 'avatar/' . $date, array(
            'maxSize'       => C('qscms_logo_max_size'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ), md5(C('visitor.uid')));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'avatar');
            $config_params['info'] = '';

        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {

            $filename = C('qscms_attach_path') . 'avatar/'. $config_params['save_path'];
            $image = new \Common\ORG\ThinkImage();
            $size = explode(',', C('qscms_avatar_size'));
            foreach ($size as $val) {
                $image->open($filename)->thumb($val, $val, 3)->save($filename . "_" . $val . "x" . $val . ".jpg");
            }

            $data['path_full'] = $config_params['show_path'];
            $data['img'] = $config_params['save_path'];

            $setsqlarr['avatars'] = $config_params['save_path'];
            $setsqlarr['photo'] = 0;
            $setsqlarr['photo_audit'] = 2;

            if (true !== $reg = D('Members')->update_user_info($setsqlarr, C('visitor'))) $this->ajaxReturn(1, $reg);
            $user_resume_list = D('Resume')->where(array('uid' => $this->userinfo['uid']))->select();
            foreach ($user_resume_list as $key => $value) {
                D('Resume')->check_resume_wx($this->userinfo['uid'], $value['id']);//更新简历完成状态
            }

            //D('TaskLog')->do_task(C('visitor'), 'upload_avatar');
            write_members_log(C('visitor'), '', '上传头像');

            $data['path_full']  = $config_params['show_path'];
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }
    /**
     * [pay_img 支付凭证上传保存]
     */
    protected function pay_img($pid=0, $fileName='',$order_id='') {

        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );
        $order_id=I('order_id','');
        $order_info=M('order')->where(['id'=>$order_id])->find();
        if(!$order_id || empty($order_info)){
            $this->ajaxREturn(1,'订单不存在');
        }
        $date = date('ym/d/');
        $result = $this->_upload($_FILES['pay_img'], 'pay_img/' . $date, array(
            'maxSize'       => C('qscms_certificate_max_size'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ), md5(C('visitor.uid')));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'pay_img');
            $config_params['info'] = '';

        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $data['img'] = $config_params['save_path'];
            $data['path_full']  = $config_params['show_path'];
            $res=M('order')->where(['id'=>$order_id])->setField('pay_img',$data['img']);
            $res===false && $this->ajaxReturn(1,'上传出错','sql:'.M('order')->getLastSql());

            //   $this->ajaxReturn(0,'上传成功');
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    /**
     * [pay_img 支付凭证上传保存]
     */
    protected function bu_jiao_pay_img($pid=0, $fileName='',$order_id='') {

        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );
        $order_id=I('order_id','');
        $order_info=M('com_bu_jiao_order')->where(['id'=>$order_id])->find();
        if(!$order_id || empty($order_info)){
            $this->ajaxREturn(1,'订单不存在');
        }
        $date = date('ym/d/');
        $result = $this->_upload($_FILES['bu_jiao_pay_img'], 'bu_jiao_pay_img/' . $date, array(
            'maxSize'       => C('qscms_certificate_max_size'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ), md5(C('visitor.uid')));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'bu_jiao_pay_img');
            $config_params['info'] = '';

        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            $data['img'] = $config_params['save_path'];
            $data['path_full']  = $config_params['show_path'];
            $res=M('com_bu_jiao_order')->where(['id'=>$order_id])->setField('pay_img',$data['img']);
            $res===false && $this->ajaxReturn(1,'上传出错','sql:'.M('com_bu_jiao_order')->getLastSql());

         //   $this->ajaxReturn(0,'上传成功');
            $this->ajaxReturn(0, L('upload_success'), $data, '', 'json');
        } else {
            $this->ajaxReturn(1, $config_params['info']);
        }
    }

    /**
     * [subject_message_img 网络招聘会聊天图片]
     */
    protected function subject_message_img() {
        $subject_id = I('post.id', 0, 'intval');
        $config_params = array(
            'upload_ok' => false,
            'save_path' => '',
            'show_path' => '',
            'info'      => ''
        );

        $date = date('ym/d/');
        $result = $this->_upload($_FILES['subject_message_img'], 'subject_message_img/' . $date, array(
            'maxSize'       => C('qscms_company_img_max'),
            'uploadReplace' => true,
            'attach_exts'   => 'bmp,png,gif,jpeg,jpg'
        ));
        if ($result['error']) {
            $config_params['upload_ok'] = true;
            $config_params['save_path'] = $date . $result['info'][0]['savename'];
            $config_params['show_path'] = attach($config_params['save_path'], 'subject_message_img');
            $config_params['info'] = '';
        } else {
            $config_params['info'] = $result['info'];
        }

        if ($config_params['upload_ok']) {
            if(C('visitor.utype') == 1){
                $company = M('CompanyProfile')->where(array('uid'=>C('visitor.uid')))->find();
                !$company && $this->ajaxReturn(0,'请先去完善企业资料呦~！');
                $arr['name'] = $company['companyname'];
                if($company['logo']){
                   $arr['head_img'] = $company['logo']; 
                }
            }else{
                $resume = M('Resume')->where(array('uid'=>C('visitor.uid')))->find();
                !$resume && $this->ajaxReturn(0,'请先去创建简历呦~！');
                $arr['name'] = $resume['fullname'];
                if($resume['photo_img']){
                   $arr['head_img'] = $resume['photo_img']; 
                }
                $arr['sex'] =$resume['sex'];
            }
            $arr['s_id'] = $subject_id;
            $arr['note_img'] = $config_params['save_path'];
            $arr['uid'] = C('visitor.uid');
            $arr['utype'] = C('visitor.utype');
            $arr['addtime'] = time();
            M('SubjectMessageLog')->add($arr);
            $data = array('path' => $config_params['show_path']);
            $this->ajaxReturn(1, L('upload_success'), $data, '', 'HTML');
        } else {
            $this->ajaxReturn(0, $config_params['info']);
        }

    }
}

?>